package com.dyrj.pig.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.jzb.po.Po6000OpenCustAcctId;
import com.dyrj.jzb.service.JzbApiService;
import com.dyrj.pig.bean.TRecordSubno;
import com.dyrj.pig.bean.TUser;
import com.dyrj.pig.bean.TYskPay;
import com.dyrj.pig.dao.TRecordSubnoMapper;
import com.dyrj.pig.dao.TUserMapper;
import com.dyrj.pig.dao.TYskPayMapper;
import com.dyrj.pig.service.TUserService;
import com.dyrj.pig.service.TYskPayService;
import com.dyrj.pig.util.StrSeqUtil;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.dyrj.ysk.configuration.SdkProperties;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**   
 * @Title: TaskServiceImpl.java 
 * @Package com.dyrj.base.system.user.service.impl
 * @Description: ()
 * @author   
 * @date 2019年4月13日 下午11:30:23
 * @version V1.0   
 */
@Service
@Log4j2
public class TUserServiceImpl implements TUserService {


    @Resource
	private TUserMapper tUserMapper;

    @Resource
	private TRecordSubnoMapper tRecordSubnoMapper;

    @Resource
	private JzbApiService jzbApiService;

    @Resource
    private UploadProperties uploadProperties;

    @Resource
    private SdkProperties sdkProperties;

	@Override
	public PageResultMsg table(TUser record) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//		record.setUserid(loginUser.getAccount());
		PageInfo<TUser> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> tUserMapper.selectList(record));
		return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
	}


	@Override
	public ResultMsg add(TUser record) {
        String msg = "";
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//        record.set(UUIDUtil.uuid());
//        record.setInsertime(new Date());
//        record.setUpdatetime(new Date());
//        record.setUserid(loginUser.getAccount());

        try {
            tUserMapper.insert(record);
            QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("xm",record.getXm());
            queryWrapper.eq("mobile",record.getMobile());
            TUser tuser_insert_ed = tUserMapper.selectOne(queryWrapper);
            String membercode = sdkProperties.getMrchcode() + StrSeqUtil.intToStr(tuser_insert_ed.getYhbm());
            tuser_insert_ed.setMembercode(membercode);
            tUserMapper.updateById(tuser_insert_ed);
        } catch (Exception e) {
        	e.printStackTrace();
            msg = e.getMessage();
        }

        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
	}


	@Override
	public ResultMsg createsubno(TUser record) {
        String msg = "";

//        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//        record.set(UUIDUtil.uuid());
//        record.setInsertime(new Date());
//        record.setUpdatetime(new Date());
//        record.setUserid(loginUser.getAccount());

        try {
            QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("yhbm",record.getYhbm());
            TUser tuser_db = tUserMapper.selectOne(queryWrapper);
            String tranNetMemberCode=tuser_db.getMembercode();
            String userNickname=tuser_db.getXm();
            String mobile=tuser_db.getMobile();
            String email="";
            Po6000OpenCustAcctId po6000OpenCustAcctId = jzbApiService.Api6000OpenCustAcctId(tranNetMemberCode,userNickname,mobile,email);
            if("000000".equals(po6000OpenCustAcctId.getTxnReturnCode())){
                tuser_db.setSubno(po6000OpenCustAcctId.getSubAcctNo());
                tUserMapper.updateById(tuser_db);

                TRecordSubno tRecordSubno = new TRecordSubno();
                tRecordSubno.setUserid(record.getYhbm()+"");
                tRecordSubno.setNickname(tuser_db.getXm());
                tRecordSubno.setMobile(tuser_db.getMobile());
                tRecordSubno.setSubno(tuser_db.getSubno());
                tRecordSubno.setCnsmrseqno(po6000OpenCustAcctId.getCnsmrSeqNo());
                tRecordSubnoMapper.insertSelective(tRecordSubno);

            }
        } catch (Exception e) {
        	e.printStackTrace();
            msg = e.getMessage();
        }

        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
	}



	@Override
	public TUser info(String id) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("yhbm",id);
        TUser tUser = tUserMapper.selectOne(queryWrapper);
		return tUser;
	}
	
	public ResultMsg update(TUser record) {
        String msg = "";
        try {
//            TYskPay record_db = tYskPayMapper.selectByPrimaryKey(record.getId());
            tUserMapper.updateById(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }

		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
	}
	
	@Override
	public ResultMsg delete(String id) {
        tUserMapper.deleteById(id);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}

}
